﻿
using Newtonsoft.Json;
using RTW.OA.Areas.System.Models;
using RTW.OA.Areas.System.Servers;
using RTW.OA.Models;
using RTW.Utils.Utils;
using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace RTW.OA.Areas.System.Controllers
{
    public class StepController : Controller
    {
	    private StepServers stepServers;
        private readonly FlowServers flowServers;
        public StepController()
		{
			stepServers = new StepServers();
            flowServers = new FlowServers();
        }
        

        // GET: System/Step
        public ActionResult Index(int? flowid)
        {
           ViewBag.flowid = flowid;
           return View();
        }

		// GET: System/Step/Edit/5
        public ActionResult Edit(int? id, int? flowid)
        {
            var model = new wf_flowstepinfo
            {
                FLOWID = flowid
            };
            if (id != null)
            {
                ViewBag.Title = "编辑工作表步骤表";
                model = GetModel(id.Value.ToString());
            }
            else
            {
                ViewBag.Title = "新建工作表步骤表";
            }
            return View(model);
        }

		// POST: System/Step/Edit/5
        [HttpPost]
        public ActionResult Edit(int? id, wf_flowstepinfo model)
        {
            
			try
            {
                if (id == null || id == 0)
                {
                    stepServers.Add(model);
                    ViewBag.msg = "新增工作表步骤表信息成功！";
                    model = new wf_flowstepinfo();
                }
                else
                {
                    var m = stepServers.GetModelById(id.Value.ToString());
                    m.STEP= model.STEP;
                    m.NAME = model.NAME;
                    m.EVENTS = model.EVENTS;
                    m.NOTE = model.NOTE; 
                    m.ALLOWUPLOAD = model.ALLOWUPLOAD;
                    stepServers.Update(m);                   
                    ViewBag.msg = "修改工作表步骤表信息成功！";
                }
            }
            catch 
            {
                ViewBag.msg = "提交出现错误！";
            }

            return View(model);
        }

		// GET: System/Step/Delete/5
        public ActionResult Delete(int id)
        {
		    if (id != 0)
            {
                stepServers.Del(id.ToString());
                ViewBag.msg = "删除工作表步骤表信息成功！";                   
            }    
            return Content("");
        }

        public ActionResult NextDelete(int id)
        {
            if (id != 0)
            {
                stepServers.NextDel(id.ToString());
                ViewBag.msg = "删除下一步骤表信息成功！";
            }
            return Content("");
        }

        // GET: System/Step
        public ActionResult Flowstepnext(int? flowid,int? stepid)
        {
            ViewBag.flowid = flowid;
            ViewBag.stepid = stepid;
            ViewBag.NAMEData = NamesData(flowid.Value.ToString());
            //ViewBag.NAMEData = "";
            //var model = new wf_flowstepinfo();
            //if (id != null)
            //{
            //    ViewBag.NAMEData = NamesData(id.Value.ToString());
            //    ViewBag.Title = "编辑下一步骤";
            //    model = GetModel(id.Value.ToString());
            //}
            //else
            //{
            //    ViewBag.Title = "新建下一步骤";
            //}
            return View();
        }
        // POST: System/Step/Edit/5
        [HttpPost]
        public ActionResult Flowstepnext(int? flowid,int? stepid, wf_flowstepnext model)
        {
            ViewBag.flowid = flowid;
            ViewBag.NAMEData = NamesData(flowid.Value.ToString());

            //var list = ViewBag.StepData ?? new List<NextStepModel>();
            try
            {
                if (model != null)
                {

                }
                //if (model != null)
                //{
                //    list.Add(new NextStepModel { STEP = model.NEXTNAME, NOTE = model.NEXTTEXT, NAME = NamesData(flowid.Value.ToString()).Find(s => s.Value == model.NEXTSTEP).Text });
                //}
                //ViewBag.StepData = list;
                //if (stepid == null || stepid == 0)
                //{
                //    //stepServers.Add(model);
                //    //ViewBag.msg = "新增下一步骤信息成功！";
                //    //model = new wf_flowstepinfo();
                //    ViewBag.msg = "请选择步骤！";
                //}
                //else
                //{
                //    string steps = "";
                //    string texts = "";
                //    if (list.Count > 0)
                //    {
                //        foreach (var item in list)
                //        {
                //            steps += item.STEP + ",";
                //            texts += item.NOTE + "§";
                //        }
                //    }

                //    var m = stepServers.GetModelById(stepid.Value.ToString());
                //    m.NEXTSTEP = steps.TrimEnd(',');
                //    m.NEXTTEXT = texts.TrimEnd('§');
                //    stepServers.Update(m);
                //    ViewBag.msg = "修改下一步骤信息成功！";

                //}
            }
            catch
            {
                ViewBag.msg = "提交出现错误！";
            }

            return View(model);
        }


        public ActionResult Flowstepauthority(int? id)
        {
            ViewBag.NAMEData = "";
            var model = new wf_flowstepinfo();
            if (id != null)
            {
                ViewBag.NAMEData = NamesData(id.Value.ToString());
                ViewBag.Title = "编辑下一步骤";
                model = GetModel(id.Value.ToString());
            }
            else
            {
                ViewBag.Title = "新建下一步骤";
            }
            return View(model);
        }
        // POST: System/Step/Edit/5
        [HttpPost]
        public ActionResult Flowstepauthority(int? id, wf_flowstepinfo model)
        {
            ViewBag.NAMEData = NamesData(id.Value.ToString());
            try
            {
                if (id == null || id == 0)
                {
                    stepServers.Add(model);
                    ViewBag.msg = "新增下一步骤信息成功！";
                    model = new wf_flowstepinfo();
                }
                else
                {
                    var m = stepServers.GetModelById(id.Value.ToString());
                    m.STEP = model.STEP;
                    m.NAME = model.NAME;
                    m.EVENTS = model.EVENTS;
                    m.NOTE = model.NOTE;
                    m.ALLOWUPLOAD = model.ALLOWUPLOAD;
                    stepServers.Update(m);
                    ViewBag.msg = "修改下一步骤信息成功！";
                }
            }
            catch
            {
                ViewBag.msg = "提交出现错误！";
            }

            return View(model);
        }

        public ActionResult Flowstepconfidentiality(int? id)
        {
            ViewBag.NAMEData = "";
            var model = new wf_flowstepinfo();
            if (id != null)
            {
                ViewBag.NAMEData = NamesData(id.Value.ToString());
                ViewBag.Title = "编辑下一步骤";
                model = GetModel(id.Value.ToString());
            }
            else
            {
                ViewBag.Title = "新建下一步骤";
            }
            return View(model);
        }
        // POST: System/Step/Edit/5
        [HttpPost]
        public ActionResult Flowstepconfidentiality(int? id, wf_flowstepinfo model)
        {
            ViewBag.NAMEData = NamesData(id.Value.ToString());
            try
            {
                if (id == null || id == 0)
                {
                    stepServers.Add(model);
                    ViewBag.msg = "新增下一步骤信息成功！";
                    model = new wf_flowstepinfo();
                }
                else
                {
                    var m = stepServers.GetModelById(id.Value.ToString());
                    m.STEP = model.STEP;
                    m.NAME = model.NAME;
                    m.EVENTS = model.EVENTS;
                    m.NOTE = model.NOTE;
                    m.ALLOWUPLOAD = model.ALLOWUPLOAD;
                    stepServers.Update(m);
                    ViewBag.msg = "修改下一步骤信息成功！";
                }
            }
            catch
            {
                ViewBag.msg = "提交出现错误！";
            }

            return View(model);
        }

        public ActionResult Flowstepwritable(int? id)
        {
            ViewBag.NAMEData = "";
            var model = new wf_flowstepinfo();
            if (id != null)
            {
                model = GetModel(id.Value.ToString());
                //ViewBag.FieldData = stepServers.GetFieldData(model.FLOWID);
                ViewBag.Title = "编辑下一步骤";
                
            }
            else
            {
                ViewBag.Title = "新建下一步骤";
            }
            return View(model);
        }
        // POST: System/Step/Edit/5
        [HttpPost]
        public ActionResult Flowstepwritable(int? id, wf_flowstepinfo model)
        {
            ViewBag.NAMEData = NamesData(id.Value.ToString());
            try
            {
                if (id == null || id == 0)
                {
                    stepServers.Add(model);
                    ViewBag.msg = "新增下一步骤信息成功！";
                    model = new wf_flowstepinfo();
                }
                else
                {
                    var m = stepServers.GetModelById(id.Value.ToString());
                    m.STEP = model.STEP;
                    m.NAME = model.NAME;
                    m.EVENTS = model.EVENTS;
                    m.NOTE = model.NOTE;
                    m.ALLOWUPLOAD = model.ALLOWUPLOAD;
                    stepServers.Update(m);
                    ViewBag.msg = "修改下一步骤信息成功！";
                }
            }
            catch
            {
                ViewBag.msg = "提交出现错误！";
            }

            return View(model);
        }


        public ActionResult Flowstepnextadd(int? stepid,int? flowid)
        {
            ViewBag.Stepid = stepid;
            ViewBag.NAMEData = NamesData(flowid.Value.ToString());
            return View();
        }

        [HttpPost]
        public ActionResult Flowstepnextadd(int? stepid, int? flowid ,wf_flowstepnext model)
        {
            ViewBag.Stepid = stepid;
            ViewBag.flowid = flowid;
            ViewBag.NAMEData = NamesData(flowid.Value.ToString());
            if (model != null)
            {
                
               var m= stepServers.GetModelById(model.NEXTSTEPID.ToString());
                model.NEXTNAME = m.NAME;
                model.NEXTSTEPNO = m.STEP;
                stepServers.AddNextStep(model);
             

            }

            return View();
        }

        #region //GetData
        public string GetTableJsonResult(string flowid)
        {
            var list = stepServers.GetData(flowid);
            return JsonConvert.SerializeObject(list);
        }
        public string GetTableNextJsonResult(string flowid)
        {
            var list = stepServers.GetNextData(flowid);
            return JsonConvert.SerializeObject(list);
        }
        public wf_flowstepinfo GetModel(string id)
        {
            var model = stepServers.GetModelById(id);
            return model;
        }


        private List<SelectListItem> NamesData(string id)
        {
            var list = stepServers.GetStepNames(id);
            list.Add(new SelectListItem() { Value = "-1", Text = "结束" });
            return list;
        }
        #endregion //GetData
    }
}